//
//  TRepeat.m
//  iTimeline
//
//  Created by Mobioneer HV 02 on 1/14/13.
//  Copyright (c) 2013 Mobioneer Co., Ltd. All rights reserved.
//

#import "TRepeat.h"

@interface TRepeat ()
{
    UISwitch *aSwitch;
}
@end

@implementation TRepeat

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self config];
}
-(void)config
{
    self.tableView.scrollEnabled = NO;
    self.tableView.hidden = YES;
    UIView *uiView = [[UIView alloc]initWithFrame:CGRectMake(236, 8, 79, 27)];
    aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(0, 0, 79, 27)];
    aSwitch.on = NO;
    [uiView addSubview:aSwitch];
    [aSwitch addTarget:self action:@selector(aSwitch:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:uiView];
    self.navigationItem.title = @"Repeat";
    int count=0;
    for (NSString *str in self.week) {
        int i = [str intValue];
        if(i==1) count++;
    }
    if(count==0)
    {
        [_week replaceObjectAtIndex:0 withObject:@"1"];
        [_week replaceObjectAtIndex:1 withObject:@"1"];
        [_week replaceObjectAtIndex:2 withObject:@"1"];
        [_week replaceObjectAtIndex:3 withObject:@"1"];
        [_week replaceObjectAtIndex:4 withObject:@"1"];
        [_week replaceObjectAtIndex:5 withObject:@"1"];
        [_week replaceObjectAtIndex:6 withObject:@"1"];
    }
    else if(self.aSwit)
    {
        aSwitch.on = YES;
        self.tableView.hidden = NO;
    }
}
-(IBAction)aSwitch:(id)sender
{
    if(aSwitch.on)
    {
        self.tableView.hidden = NO;
        [self.delegate repeat:YES];
    }
    else
    {
         self.tableView.hidden = YES;
        [self.delegate repeat:NO];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"Every Monday";
            if([[_week objectAtIndex:0] intValue]==1) cell.accessoryType = UITableViewCellStyleSubtitle;
            else cell.accessoryType = UITableViewCellStyleDefault;
            break;
        case 1:
            cell.textLabel.text = @"Every Tuesday";
            if([[_week objectAtIndex:1] intValue]==1) cell.accessoryType = UITableViewCellStyleSubtitle;
            else cell.accessoryType = UITableViewCellStyleDefault;
            break;
        case 2:
            cell.textLabel.text = @"Every Wednesday";
            if([[_week objectAtIndex:2] intValue]==1) cell.accessoryType = UITableViewCellStyleSubtitle;
            else cell.accessoryType = UITableViewCellStyleDefault;
            break;
        case 3:
            cell.textLabel.text = @"Every Thursday";
            if([[_week objectAtIndex:3] intValue]==1) cell.accessoryType = UITableViewCellStyleSubtitle;
            else cell.accessoryType = UITableViewCellStyleDefault;
            break;
        case 4:
            cell.textLabel.text = @"Every Friday";
            if([[_week objectAtIndex:4] intValue]==1) cell.accessoryType = UITableViewCellStyleSubtitle;
            else cell.accessoryType = UITableViewCellStyleDefault;
            break;
        case 5:
            cell.textLabel.text = @"Every Saturday";
            if([[_week objectAtIndex:5] intValue]==1) cell.accessoryType = UITableViewCellStyleSubtitle;
            else cell.accessoryType = UITableViewCellStyleDefault;
            break;
        case 6:
            cell.textLabel.text = @"Every Sunday";
            if([[_week objectAtIndex:6] intValue]==1) cell.accessoryType = UITableViewCellStyleSubtitle;
            else cell.accessoryType = UITableViewCellStyleDefault;
            break;
        default:
            break;
    }
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int count=0;
    for (NSString *str in _week) {
        int i = [str intValue];
        if(i==1) count++;
    }
    switch (indexPath.row) {
        case 0:
            if([[_week objectAtIndex:0] intValue]==0)
                [_week replaceObjectAtIndex:0 withObject:@"1"];
            else if(count != 1)
                 [_week replaceObjectAtIndex:0 withObject:@"0"];
            break;
        case 1:
            if([[_week objectAtIndex:1] intValue]==0)
                 [_week replaceObjectAtIndex:1 withObject:@"1"];
            else if(count != 1)
                 [_week replaceObjectAtIndex:1 withObject:@"0"];
            break;
        case 2:
            if([[_week objectAtIndex:2] intValue]==0)
                [_week replaceObjectAtIndex:2 withObject:@"1"];
            else if(count != 1)
                [_week replaceObjectAtIndex:2 withObject:@"0"];
            break;
        case 3:
            if([[_week objectAtIndex:3] intValue]==0)
                [_week replaceObjectAtIndex:3 withObject:@"1"];
            else if(count != 1)
                [_week replaceObjectAtIndex:3 withObject:@"0"];
            break;
        case 4:
            if([[_week objectAtIndex:4] intValue]==0)
                [_week replaceObjectAtIndex:4 withObject:@"1"];
            else if(count != 1)
                [_week replaceObjectAtIndex:4 withObject:@"0"];
            break;
        case 5:
            if([[_week objectAtIndex:5] intValue]==0)
                [_week replaceObjectAtIndex:5 withObject:@"1"];
            else if(count != 1)
                [_week replaceObjectAtIndex:5 withObject:@"0"];
            break;
        case 6:
            if([[_week objectAtIndex:6] intValue]==0)
                [_week replaceObjectAtIndex:6 withObject:@"1"];
            else if(count != 1)
                [_week replaceObjectAtIndex:6 withObject:@"0"];
            break;
        default:
            break;
    }
   
    [tableView reloadData];
}

@end
